<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script src="hash.js"></script>
    <script>
      var evalRPN = function (tokens) {
        // 还得是栈来解决
        // 便利数组到运算符，从栈顶取两个数，用这个运算符进行运算，然后将运算后的值再压入栈
        // 用一个 set 来存运算符
        let set = new Set(["*", "/", "+", "-"]);
        // stack 来模拟栈
        let stack = [];
        for (let i = 0; i < tokens.length; i++) {
          // 判断是否是运算符
          if (set.has(tokens[i])) {
            // 从栈顶取两个数值进行运算
            let y = stack.pop();
            let x = stack.pop();
            let result = 0;
            // 用此运算符进行运算
            switch (tokens[i]) {
              case "*":
                result = x * y;
                break;
              case "/":
                // '/' 要向下取值
                result = Math.floor(x / y);
                break;
              case "+":
                // 将字符串转成数字
                result = x + y;
                break;
              case "-":
                result = x - y;
                break;
            }
            // 将运算结果存入栈
            stack.push(result);
          } else {
            // 不是运算符就存入栈
            stack.push(+tokens[i]);
          }
        }
        return stack.pop();
      };

      evalRPN([
        "10",
        "6",
        "9",
        "3",
        "+",
        "-11",
        "*",
        "/",
        "*",
        "17",
        "+",
        "5",
        "+",
      ]);
    </script>
  </body>
</html>
